package mobi.weq.servlets;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mobi.weq.dao.UserDao;
import mobi.weq.model.User;

public class UserServlet extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.getRequestDispatcher("/jsp/signup.jsp").forward(req, resp);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String username = req.getParameter("username");
		String email = req.getParameter("email");
		String city = req.getParameter("city");
		String country = req.getParameter("country");
		String gender = req.getParameter("gender");
		String age_group = req.getParameter("age_group");
		String password = req.getParameter("password");

		User us = new User();
		us.setUserName(username);
		us.setEmail(email);
		us.setCity(city);
		us.setCountry(country);
		us.setGender(gender);
		us.setAgeGroup(age_group);
		us.setPassword(password);

		UserDao ud = new UserDao();
		try {
			ud.addUser(us);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		HttpSession session = req.getSession(true);
		session.setAttribute("user", us);
		resp.sendRedirect("home");
		// req.getRequestDispatcher("user.jsp").forward(req, resp);
	}
}
